package com.example.asus.yuejian_10.fragment;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.base.BaseFragment;
import com.example.asus.yuejian_10.bean.ClubList;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.asus.yuejian_10.base.BaseActivity.clubid;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubInfoFragment extends BaseFragment {
    private TextView club_intro;
    private TextView club_phone;
    private TextView club_address;
    private TextView club_district;
    private Button club_tel;
    private String phonenum;

    private RetrofitSeveice service;

    public ClubInfoFragment() {
        // Required empty public constructor
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        service = retrofit.create(RetrofitSeveice.class);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        club_intro= (TextView) view.findViewById(R.id.club_intro);
        club_phone= (TextView) view.findViewById(R.id.club_phone);
        club_address= (TextView) view.findViewById(R.id.club_address);
        club_district= (TextView) view.findViewById(R.id.club_district);
        club_tel= (Button) view.findViewById(R.id.club_tel);

        Toast.makeText(getActivity(), clubid, Toast.LENGTH_SHORT).show();
        Call<ClubList> call = service.getclub(clubid);
        call.enqueue(new Callback<ClubList>() {
            @Override
            public void onResponse(Call<ClubList> call, Response<ClubList> response) {
                ClubList clubList = response.body();

                club_intro.setText(clubList.getClub_contact());
                club_phone.setText(clubList.getClub_tel());
                club_address.setText(clubList.getCity_name());
                club_district.setText(clubList.getDistrict_name());

                phonenum = clubList.getClub_tel();
            }

            @Override
            public void onFailure(Call<ClubList> call, Throwable t) {

            }
        });

        club_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));

                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club_info, container, false);
    }

}
